/**
  ******************************************************************************
  * @file    system_sym32l010.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   system_sym32l010 file.
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "system_sym32l010.h"


/******************************************************************************/
/* System Clock Frequency (Core Clock) Variable according CMSIS               */
/******************************************************************************/
uint32_t SystemCoreClock = 4000000;

void SystemCoreClockUpdate(void)
{
    //SystemCoreClock = 4000000;
}


/*******************************************************************************
  * @brief  Setup the microcontroller system. Initialize the System and update
  *         the SystemCoreClock variable.
  *
  * @param  none
  * @retval none
  */
void SystemInit(void)
{
    if (TRIMCODE_HSI_VALUE != 0xFFFF)
    {
        // Load RCOSC TrimCode in this.
        SYM_SYSCTRL->HSI_f.TRIM = TRIMCODE_HSI_VALUE + 3;
        SYM_SYSCTRL->LSI_f.TRIM = TRIMCODE_LSI_VALUE;
        
        // Default 4MHz
        SystemCoreClock = 4000000;
    }
    else
    {
        // NO Trimcode
        while (1);
    }
}


/*******************************************************************************
  * @brief FirmwareDelay Funcation
  *
  * @param  DlyCnt: DlyCnt
  * @return none
  * @note   延时时间: T(ms) = (DlyCnt * W / F) * 1000 = nms
  *                   DlyCnt = n * F / 1000 / W
  *                   F：系统主频HCLK
  *                   W：FlashWait=0 时 W=10
  *                      FlashWait=1 时 W=15
  *                      FlashWait=2 时 W=20
  *
  *         例如: 需要延时100ms @ Hclk = 48MHz
  *         计算：DlyCnt = 100 * 48000000 / 1000 / 15 = 320000
  *
  */
#if defined(__CC_ARM)
__inline __asm void
FirmwareDelay(uint32_t DlyCnt)
{
        CMP    R0, #0
        BEQ    EXIT
LOOP
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
        NOP
        SUBS   R0, R0, #1
        BNE    LOOP
EXIT
        BX     LR
}

#elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
void __attribute__((naked))
FirmwareDelay(uint32_t ulCount)
{
  __asm(
        "CMP    R0, #0\n"
        "BEQ    EXIT\n"
    "LOOP:"
        "NOP    \n"
        "NOP    \n"
        "NOP    \n"
        "NOP    \n"
        "NOP    \n"
        "NOP    \n"
        "NOP    \n"
        "SUBS   R0, #1\n"
        "BNE    LOOP\n"
    "EXIT:"
        "BX     LR");
}

#elif defined(__ICCARM__)
void
FirmwareDelay(uint32_t ulCount)
{
  __asm(
        "CMP    R0, #0\n"
        "BEQ    EXIT\n"
    "LOOP:"
        "NOP    \n"
        "NOP    \n"
        "NOP    \n"
        "NOP    \n"
        "NOP    \n"
        "NOP    \n"
        "NOP    \n"
        "SUBS   R0, R0, #1\n"
        "BNE.N  LOOP\n"
    "EXIT:"
        "BX     LR");
}

#elif defined(__GNUC__)
void __attribute__((naked))
FirmwareDelay(uint32_t ulCount)
{
  __asm(
        "CMP    R0, #0\n"
        "BEQ    EXIT\n"
    "LOOP:"
        "NOP    \n"
        "NOP    \n"
        "NOP    \n"
        "NOP    \n"
        "NOP    \n"
        "NOP    \n"
        "NOP    \n"
        "SUB    R0, #1\n"
        "BNE    LOOP\n"
    "EXIT:"
        "BX     LR");
}

#else
void FirmwareDelay(uint32_t DlyCnt)
{
    __IO uint32_t tmp32 = DlyCnt;
    while (tmp32)
    {
        tmp32--;
    }
}

#endif /* __CC_ARM */


/*******************************************************************************
  * @brief  clear memory
  *
  * @param  start:  addr
  * @param  memory: size(byte)
  * @retval none
  */
void MemClr(void *pu8Address, uint32_t ByteCnt)
{
    uint8_t *pu8Addr = (uint8_t *)pu8Address;

    if (NULL == pu8Addr)
    {
        return;
    }

    while (ByteCnt--)
    {
        *pu8Addr++ = 0;
    }
}


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
